package com.navinfo.parkingnavi.Adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.navinfo.parkingnavi.R;

import java.util.ArrayList;

/*
 *com.navinfo.parkingnavi.Adapter
 *zhjch
 *2020/5/14
 *15:04
 *说明（联想搜索poi适配器）
 */
public class FloorListAdapter extends RecyclerView.Adapter<FloorListAdapter.ViewHolder> {
    //poi数据
    private ArrayList<String> mList = new ArrayList<>();;
    //点击监听
    private OnItemClickListener mListener;
    private View selectIndex;
    private String flrId;
    private String parkingId;
    public interface OnItemClickListener {
        //显示poi
        void onSelectFloor(String parkingId, String floorId);
    }

    public void addOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    /**
     * 传递数据并刷新
     *
     * @param list
     */
    public void setData(ArrayList<String> list, String parkingId, String floorId) {
        this.parkingId = parkingId;
        this.flrId = floorId;
        mList = list;
        notifyDataSetChanged();
    }

    /**
     * 获取所有数据
     *
     * @return
     */
    public ArrayList<String> getData() {
        return mList;
    }

    @NonNull
    @Override
    public FloorListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_floors, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(@NonNull FloorListAdapter.ViewHolder holder, int position) {
        if (mList != null && mList.size() > position) {
            final String name = mList.get(position);
            if(TextUtils.equals(flrId,name)){
                holder.name.setSelected(true);
                selectIndex = holder.name;
            }else{
                holder.name.setSelected(false);
            }
            holder.name.setText(name);
            holder.name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!TextUtils.equals(flrId,name)){
                        v.setSelected(true);
                        selectIndex.setSelected(false);
                        selectIndex = v;
                        flrId = name;
                        if (mListener != null) {
                            mListener.onSelectFloor(parkingId,flrId);
                        }
                    }
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        if (mList != null)
            return mList.size();
        return 0;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name;//名称

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.adapter_floor);
        }
    }
}
